using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
namespace DAL
{
   public class tbDiaDiemDAL
    {
       private SqlConnection cn = null;
        public tbDiaDiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

       public tbDiaDiemDAL(tbDiaDiemDAL diadiem)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbDiaDiem diadiem)
        {
            SqlCommand cm = new SqlCommand("tbDiaDiem_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", diadiem.madiadiem);
            cm.Parameters.AddWithValue("@tendiadiem", diadiem.tendiadiem);
            cm.Parameters.AddWithValue("@diachi", diadiem.diachi);
            cm.Parameters.AddWithValue("@bando", diadiem.bando);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDiaDiem th)
        {
            SqlCommand cm = new SqlCommand("tbDiaDiem_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", th.madiadiem);
            cm.Parameters.AddWithValue("@tendiadiem", th.tendiadiem);
            cm.Parameters.AddWithValue("@diachi", th.diachi);
            cm.Parameters.AddWithValue("@bando", th.bando);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbDiaDiem_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", iD);
            cm.ExecuteNonQuery();
        }
        public tbDiaDiemCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbDiaDiem_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDiaDiemCollection ddCollection = new tbDiaDiemCollection();

            while (dr.Read())
            {
                tbDiaDiem th = new tbDiaDiem();
                th.madiadiem = int.Parse(dr.GetValue(0).ToString());
                th.tendiadiem = dr.GetValue(1).ToString();
                th.diachi = dr.GetValue(2).ToString();
                th.bando = dr.GetValue(3).ToString();
                ddCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ddCollection;
        }
    }
}
